package com.googlecode.ozonbooksfinder.domain
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.core.IUID;
	import mx.utils.ObjectUtil;
	import mx.utils.UIDUtil;
	
	[Bindable]
	[RemoteClass(alias="com.googlecode.ozonbooksfinder.domain.Book")]
	public class Book extends EventDispatcher implements IUID
	{
		public function Book()
		{
			uid = UIDUtil.createUID();
		}
		
		public function addExtendedDetails (details:Book):void
		{
			var classInfo:Object = ObjectUtil.getClassInfo(details, null,
				{includeReadOnly: false, includeTransient: false});
			    
			var properties:Array = classInfo.properties;
			
			for each (var property:String in properties)
			{
				var data:Object = details [property];
				if (data != null)
				{
					var type:String = typeof(data);
					switch (type)
					{
						case "boolean":
						{
							this [property] = details [property];
							break;
						}
						case "number":
						{
							if (!isNaN(details [property]) && (details [property] > 0))
							{
								this [property] = details [property];
							}
							break;
						}
						
						case "string":
						{
							if (details [property].length > 0)
							{
								this [property] = details [property];
							}
							break;
						}
						
						case "object":
						{
							if (data is Date)
							{
								this [property] = details [property];
							}
							break;
						}
					}
				}
			}
			if (!_isExtendedDetailsAdded)
			{
				_isExtendedDetailsAdded = true;
				dispatchEvent(new Event ("isExtendedDetailsAddedChange"));
			}
		}
		
		private var _isExtendedDetailsAdded:Boolean = false;
		[Bindable(event="isExtendedDetailsAddedChange")]
		[Transient]
		public function get isExtendedDetailsAdded ():Boolean
		{
			return _isExtendedDetailsAdded;
		}

		private var _uid:String;
		[Transient]
		public function get uid():String
		{
			return _uid;
		}
		
		public function set uid(value:String):void
		{
			_uid = value;
		}
		
		/**
		 * идентификатор товара
		 */
		public var id:int = -1;
		
		/**
		 * наименование товара
		 */
		public var name:String;
		
		/**
		 * дополнительное наименование товара
		 */
		public var otherName:String;
		
		/**
		 * автор
		 */
		public var author:String;
		
		/**
		 * год издания
		 */
		public var year:int = -1;
		
		/**
		 * краткая аннотация
		 */
		public var annotation:String;
		
		/**
		 * путь к картинке товара
		 */
		public var imagePath:String;
		
		/**
		 * тип товара
		 */
		public var itemType:String;
		
		/**
		 * дата поступления
		 */
		public var date:Date;
		
		/**
		 * цена
		 */
		public var price:Number;
		
		/**
		 * товар в комплекте
		 */
		public var complekt:int = -1;
		
		/**
		 * новинка
		 */
		public var isNew:Boolean;
		
		/**
		 * производитель
		 */
		public var manufacturer:String;
		
		/**
		 * объем (страниц)
		 */
		public var pages:int = -1;
		
		/**
		 * код ISBN
		 */
		public var isbnCode:String;
		
		/**
		 * товар рекомендован нашими экспертами
		 */
		public var isRecommended:Boolean;
		
		/**
		 * вес товара
		 */
		public var weight:int = -1;
		
		/**
		 * путь к странице с полной деталировкой товара
		 */
		public var url:String;
		
		/**
		 * наличие
		 */
		public var availability:String;
		
		/**
		 * 
		 */
		public var availabilityDate:Date;
		
		/**
		 * 
		 */
		public var specialPrice:Number;
		
		/**
		 * 
		 */
		public var hasMerchant:int = -1;
		
		/**
		 * 
		 */
		public var digitalTypeID:int = -1;
	}
}